<template>
  <div class="permission-container">
    <div class="app-container">
      <!-- 表格 -->
      <el-card>
        <div style="text-align: right; margin-bottom: 20px">
          <el-button type="primary" size="small" @click="addPermissionFn(1,'0')">添加权限</el-button>
        </div>

        <el-table border :data="permissionList" row-key="id">
          <el-table-column label="名称" prop="name" />
          <el-table-column label="标识" prop="code" />
          <el-table-column label="描述" prop="description" />
          <el-table-column label="操作">
            <template v-slot="scope">
              <!-- 子项不具有添加功能 -->
              <el-button v-if="scope.row.pid === '0'" type="text" @click="addPermissionFn(2,scope.row.id)">添加</el-button>
              <el-button type="text" @click="edit(scope.row.id)">编辑</el-button>
              <el-button type="text" @click="delFn(scope.row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-card>
      <!-- 弹窗 -->
      <per-dialog ref="perDialog" :show-dialog.sync="showPerDialog" :parent-id="parentId" :is-edit="isEdit" :origin-list="originList" @confirmEV="confirmFn" />
    </div>
  </div>
</template>

<script>
import { getPermissionListAPI, addPermissionAPI, getPermissionDetailAPI, updatePermissionAPI, delPermissionAPI } from '@/api'
import { transTree } from '@/utils/transTree.js'
import perDialog from '@/views/permission/components/perDialog.vue'
export default {
  name: 'Permission',
  components: {
    perDialog
  },
  data() {
    return {
      permissionList: [], // 所有权限点列表
      showPerDialog: false,
      type: '',
      pid: '',
      isEdit: false,
      parentId: '',
      originList: []
    }
  },
  created() {
    this.getPermissionListFn()
  },
  methods: {
    // 获取所有权限点列表
    async getPermissionListFn() {
      const res = await getPermissionListAPI()
      // console.log(res)
      this.originList = res.data.map(item => (
        {
          id: item.id,
          name: item.name,
          code: item.code,
          pid: item.pid
        }
      ))
      // 树形控件处理数据
      this.permissionList = transTree(res.data, '0')
    },
    // 添加权限点
    addPermissionFn(type, pid) {
      this.isEdit = false
      this.showPerDialog = true
      this.parentId = pid
      this.pid = pid
      this.type = type
    },
    // 权限点改变(新增/编辑)
    async confirmFn(dataObj) {
      if (this.isEdit) {
        // 编辑状态下,已经通过接口获取到当前权限点的type和pid,所以不再需要手动传入type和pid
        const res = await updatePermissionAPI(dataObj)
        this.$message.success(res.message)
      } else {
        const res = await addPermissionAPI({
          ...dataObj,
          type: this.type,
          pid: this.pid
        })
        // console.log(res)
        this.$message.success(res.message)
      }
      this.getPermissionListFn()
    },
    // 编辑权限点
    async edit(id) {
      this.isEdit = true
      this.parentId = id
      this.showPerDialog = true
      const res = await getPermissionDetailAPI(id)
      // console.log(res)
      this.$refs.perDialog.formData = res.data
    },
    async delFn(id) {
      await delPermissionAPI(id)
      this.getPermissionListFn()
    }
  }
}
</script>

<style lang="scss" scoped></style>
